<?
/**
 * 数据库同步工具 ajax操作
 * by kom 09.10
 */
define('DC', true);
include_once('../common.php');

$act = !isset($_REQUEST['act']) ? '' : trim($_REQUEST['act']);

//标准数据库比新数据库多的情况
//三个步骤, 1.表名对比  2.表名相同进行字段对比  3.表名相同且在指定中的表进行条数对比
if ($act == 'comparedb') {
	if (empty($_POST['dbId'])) {
		exit('请选择要比较的库!');
	}
	$first = $_REQUEST['first'];
	$first == 1 ? $_SESSION['num'] = 0 : $_SESSION['num'] += 1;
	
	//获取数据库id 连接数据库 kom
	$dbmainId   = $_POST['dbmainId'];
	$dbId       = $_POST['dbId'];
	
	$dbIds[]      = $dbId;
	$dbIds[]    = $dbmainId;
	$g_db       = mysqlConnect($dbIds);
	//获取数据库相关信息
	
	//读取出主库所有的表
	if ($first == 1){
		$sql = "SHOW TABLES";
		$row = $g_db[$dbmainId]->query($sql);
		while( $result = $row -> fetch_row() ){
			$maintableArr[] = $result[0];
		}
		$count = count($maintableArr);
		if ($count == 0) {
			exit('<font color=red>主库为空,请检查!</font>');
		}
		$_SESSION['maintable'] = $maintableArr;
	}else{
		$maintableArr = $_SESSION['maintable'];
		$count = count($maintableArr);
	}
	if (isset($maintableArr[$_SESSION['num']])){
		$tablename = $maintableArr[$_SESSION['num']];
	}else{
		exit(json_encode(array('state'=>'1', 'content'=>'', 'num'=>'end')));
	}
	

	if (!stristr($tablename, 'backup0_0')) {
		//读取每张表的字段		
		$sql = "DESCRIBE $tablename";
		$mainrow = $g_db[$dbmainId]->query($sql);
		if (!mysqli_errno($g_db[$dbmainId])){
		while($result2 = $mainrow->fetch_array()){
			//print_r($result2);exit;
			//加入索引
			$sy = '';
			if (!empty($result2[3])) {
				$sy = '<font color=green>['.$result2[3].']</font>';
			}
			$mainArr[] = array($result2[0], '<font color=blue>'.$result2[1].'</font> '.$sy);
		}
		}else{
			$mainArr = array();
		}
		
		$subrow = $g_db[$dbId]->query($sql);
		if (!mysqli_errno($g_db[$dbId])){
			while($result2 = $subrow->fetch_array()){
				//加入索引
				$sy = '';
				if (!empty($result2[3])) {
					$sy = '<font color=green>['.$result2[3].']</font>';
				}
				$subArr[] = array($result2[0], '<font color=blue>'.$result2[1].'</font> '.$sy);
			}
		}else{
			$subArr = array();
		}
		$field_lack  = @array_udiff($mainArr, $subArr, 'myDiff');//字段缺少
		$field_more  = @array_udiff($subArr, $mainArr, 'myDiff');//字段多余
		
		//用于同步字段时判断
		$_SESSION['diff'][$dbId]['field_lack'][$tablename] = $field_lack;
		$_SESSION['diff'][$dbId]['field_more'][$tablename] = $field_more;
		
		//print_r($subArr);exit;
		$content = '';
		$yz = 0;
		$content .= '<tr><td><input type="checkbox" name="dbids_'.$dbId.'" value="'.$tablename.'" /></td><td>'.$tablename.'</td>';
		if( count($mainArr)>0 && count($subArr)==0 ){
			$content .= '<td><font color="red">缺少</font></td><td>未比较</td><td>未比较</td></tr>';
			$_SESSION['diff'][$dbId]['lack'][] = $tablename;
		}elseif ( count($mainArr)==0 && count($subArr)>0 ){
			$content .= '<td><font color="red">多余</font></td><td>未比较</td><td>未比较</td></tr>';
			$_SESSION['diff'][$dbId]['more'][] = $tablename;
		}else{
			$_SESSION['diff'][$dbId]['interset'][] = $tablename;
			if( count($field_lack)>0 ){
				$content .= '<td><font color="red">存在</font></td><td>';
				foreach ($field_lack as $k => $v){
					$content .= $v[0].' '.$v[1].'<br/>';
				}
			}else{
				$content .= '<td>存在</td><td>';
				$content .= '一致';
				$yz += 1;
			}
			$content .= '</td><td>';
			if( count($field_more)>0 ){
				foreach ($field_more as $k => $v){					
					$content .= $v[0].' '.$v[1].'<br/>';
				}
			}else{
				$content .= '一致';
				$yz += 1;
			}
			$content .= '</td></tr>';		
			if ($yz == 2){
				$content = '';//一致的不显示
			}
		}

		if ($_SESSION['num'] >= $count-1){
			if ($_SESSION['num'] == 0){
				exit(json_encode(array('state'=>'1', 'content'=>$content, 'num'=>'all', 'table'=>$tablename)));
			}else{
				exit(json_encode(array('state'=>'1', 'content'=>$content, 'num'=>'end', 'table'=>$tablename)));
			}
		}else{
			exit(json_encode(array('state'=>'1', 'content'=>$content, 'num'=>$_SESSION['num'], 'table'=>$tablename, 'nexttable'=>$maintableArr[$_SESSION['num']+1])));
		}
	}else{
		exit(json_encode(array('state'=>'0', 'content'=>$sql, 'num'=>$_SESSION['num'], 'table'=>$tablename, 'nexttable'=>$maintableArr[$_SESSION['num']+1])));
	}
	

}
?>